<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017/9/21
 * Time: 15:28
 */

namespace App\Http\Controllers\Business;

use App\Base\BaseFunc;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class BusinessController extends Controller
{
    public function businessLogin()
    {
        return view('/business.businessLogin');
    }

    public function _businessLogin(BaseFunc $baseFunc)
    {
        $input = Input::only('business_phone');
        $password = Input::only('business_password');
        $input['business_password'] = md5($password['business_password']);
        $business = DB::table('business')
            ->where('business_phone','=',$input['business_phone'])
            ->value('business_password');
        if($business == NULL)
        {
            $baseFunc->setRedirectMessage(false,'您还没有注册成为卖家或没有通过审核，请先注册',NULL,'/businessRegister');
        }
        else
        {
            $businessData = DB::table('business')
                ->where('business_phone','=',$input['business_phone'])
                ->where('business_password','=',$input['business_password'])
                ->first();
            if($businessData == NULL)
            {
                $baseFunc->setRedirectMessage(false,'密码错误',NULL,'/businessLogin');
            }
            else
            {
                if($businessData->business_status == 0)
                {
                    $baseFunc ->setRedirectMessage(false,"您提交的信息正在审核中，请稍后登录",NULL,"/businessLogin");
                }
                else
                {
                    session(['business_id' => $businessData->business_id]); //获取商家ID
                    session(['business_name' => $businessData->business_name]); //获取店铺名
                    $baseFunc->setRedirectMessage(true,'欢迎来到寻医问药！',NULL,'/myGoods');
                }
            }
        }
    }

    public function businessRegister()
    {
        return view('/business.businessRegister');
    }

    public function _businessRegister(BaseFunc $baseFunc)
    {
        $businessData = Input::only('business_name','business_phone','business_password');
        $businessData['business_status'] = 0;
        $businessData['business_password'] = md5($businessData['business_password']);
        $phoneCheck = DB::table('business')
            ->where('business_phone','=',$businessData['business_phone'])
            ->get();
        if($phoneCheck)
        {
            $baseFunc->setRedirectMessage(false,'此手机号已被注册，请重新注册',NULL,'/businessRegister');
        }
        else
        {
            $shopCheck = DB::table('business')
                ->where('business_name','=',$businessData['business_name'])
                ->get();
            if($shopCheck)
            {
                $baseFunc->setRedirectMessage(false,'此店铺名称已被注册，请重新注册',NULL,'/businessRegister');
            }
            else
            {
                DB::table('business')->insert($businessData);
                $baseFunc->setRedirectMessage(true,'恭喜您注册商铺申请成功，请等待审核',NULL,'/businessLogin');
            }
        }
    }

    public function businessLogout(BaseFunc $baseFunc)
    {
        session(["business_id"=>NULL]);
        $baseFunc ->setRedirectMessage(true,"您已退出登录",NULL,"/businessLogin");
    }
}